package com.android.datn.activities.offline;

import java.util.ArrayList;

import com.android.datn.R;
import com.android.datn.R.layout;
import com.android.datn.R.menu;
import com.android.datn.activities.ANews;
import com.android.datn.activities.GActivityHome;
import com.android.datn.activities.GActivityNews;
import com.android.datn.activities.GActivityTop;
import com.android.datn.activities.offline.AAlbum.AsynAlbum;
import com.android.datn.adapter.AlbumAdapter;
import com.android.datn.adapter.ArtistAdapter;
import com.android.datn.customs.view.SlideHolder;
import com.android.datn.helper.DBBinding;
import com.android.datn.objects.Album;
import com.android.datn.objects.Artist;
import com.android.datn.repositories.MusicRepository;
import com.android.datn.services.AudioPlayer;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.app.TabActivity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TextView;

public class AArtist extends Activity implements OnClickListener,
		OnItemClickListener {
	private ListView mLview;
	private ArtistAdapter mAdapter;
	private ArrayList<Artist> mArrayArtist;
	private MusicRepository mMusicRespository;
	private ProgressDialog mProgressDialog;
	public static AArtist artist;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_offline_artist);
		init();
	}

	public void init() {
		artist = this;
		mLview = (ListView) findViewById(R.id.lv_artist);
		mArrayArtist = new ArrayList<Artist>();
		mAdapter = new ArtistAdapter(this, R.layout.view_album_offline,
				mArrayArtist);
		mLview.setAdapter(mAdapter);
		mLview.setOnItemClickListener(this);
		mMusicRespository = new MusicRepository();
		mProgressDialog = new ProgressDialog(getParent().getParent());
		mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		mProgressDialog.setMessage("Please wait... Uploading.");
		mProgressDialog.setCancelable(false);
		AsynArtist asyn = new AsynArtist();
		asyn.execute();
	}

	public void onItemReClick() {
		mLview.setAdapter(mAdapter);
		mLview.setOnItemClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_download:

			break;

		default:
			break;
		}
	}

	@SuppressWarnings("deprecation")
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		Intent t = new Intent(this, ASongOfArtist.class);
		Bundle bundle = new Bundle();
		bundle.putSerializable(DBBinding.ARTIST, mArrayArtist.get(arg2));
		t.putExtra(DBBinding.BUNDLEARTIST, bundle);
		View view1 = GActivityOffline.group
				.getLocalActivityManager()
				.startActivity("Offline",
						t.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
				.getDecorView();
		GActivityOffline.group.replaceView(view1);
	}

	public class AsynArtist extends AsyncTask<Void, Void, Void> {
		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			for (int i = 0; i < mMusicRespository.findAllArtist(AArtist.this)
					.size(); i++) {
				mArrayArtist.add(mMusicRespository.findAllArtist(AArtist.this)
						.get(i));
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			mProgressDialog.dismiss();
			mAdapter.notifyDataSetChanged();
			super.onPostExecute(result);
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			mProgressDialog.show();
			super.onPreExecute();
		}
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		GActivityOffline.group.back();
	}
}
